# Clean up
rm(list = ls())

# Set working directory; please set your own here
setwd("~/Dropbox/Fragmentation/fragmentation_replication_bjps/")

library(tidyverse)
library(readxl)

# Import the election-level dataset
first_stage <- read.dta13("Data/fragmentation_electionlevel.dta",generate.factors=TRUE)

# Import data with main analyses
data <- read.dta("Results/analyses_final.dta")

# Creating a theme for the plot
theme_base <- 
  theme_minimal(base_size=9)  + 
  theme(legend.position = c(0.85, 0.1),
        axis.text=element_text(size=12),axis.title.x=element_text(size=12),axis.title.y=element_text(size=12),
        plot.title = element_text(size=12, hjust= 0.5),
        legend.text = element_text(size=12))

# Plotting ENPP against ENPP added by parties in the bandwidth
enpp_log_plot <- ggplot(first_stage, aes(x = enppadd_bw50_log, y = enpp)) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "red",size = .5) +
  geom_point(alpha = 0.6) +
  stat_smooth(method = "lm", aes(colour = "Linear model")) +
  stat_smooth(method = "loess", linetype = "longdash", aes(colour = "LOESS")) +
  scale_colour_manual(name = " ", values = c("blue", "green3")) +
  scale_y_continuous(limits = c(-3, 10)) +
  theme_base +
  labs(x = "Effective number of parl parties added by parties within the bandwidth (logged)", y = "Effective number of parliamentary parties")
enpp_log_plot

# Plot distribution of f-statistics
firststage_distr_enpp_log1 <- data %>%
  filter(Sample == "Whole") %>%
  filter(FE == "None") %>%
  filter(id == "No. parties just above (logged)") %>%
  filter(Predictor == "ENPP") %>%
  ggplot +
  geom_density(aes (x = fstat), fill = "steelblue", color = "blue", alpha = 0.5) +
  scale_x_continuous(name = "F-test", limits = c(15, 27.5)) +
  scale_y_continuous(name = "Density") +
  geom_vline(aes(xintercept = mean(fstat)) ,col = 'red',size = 1, linetype = "dashed") +
  theme_bw() 
firststage_distr_enpp_log1

# Put the two plots together
firststage_log1 <- grid.arrange(
  enpp_log_plot,
  firststage_distr_enpp_log1,
  nrow = 1,
  ncol = 2
)
firststage_log1

# Save final plot
ggsave("Plots/figa6.png", plot = firststage_log1, width = 40, height = 20, units = "cm")